import { monitoryList, manufacturerList, monitoryPreview } from '../../api/index'
import { getYMDHMS } from '../../utils/util'
Page({

  /* 页面的初始数据 */
  data: {
    inputValue: '',
    page: 0,    //页码
    is: true,   //控制是否还能切换下一页
    list: [],
    idIndex: 1,
    tabList: [],
    active: 0,
    show: false,
    src: '',
    info: {},
  },
  statechange(e) {
    console.log('live-player code:', e.detail.code)
  },
  error(e) {
    console.error('live-player error:', e.detail.errMsg)
  },
  yulan(e) {
    // this.setData({
    //   src: e.currentTarget.dataset.url,
    //   show: true,
    // })
    // let ctx = wx.createLivePlayerContext('player')
    // ctx.play({
    //   success: res => {
    //     console.log('play success')
    //   },
    //   fail: res => {
    //     console.log('play fail')
    //   }
    // })
    wx.navigateTo({
      url: '/pages/video/index?src=' + encodeURIComponent(e.currentTarget.dataset.url)
    })
  },
  onClose() {
    this.setData({
      show: false,
      src: ''
    })
  },
  tomanufacturerList() {
    manufacturerList({ param: { type: '3' } }).then(res => {
      this.setData({
        tabList: res.recordList,
        active: 0
      })
      this.getList()
    }).catch(err => {
      console.log(err)
    })
  },
  onChange(e) {
    this.setData({ active: e.detail.name, is: true, page: 0, list: [] })
    this.getList()
  },
  // 列表
  getList() {
    if (!this.data.is) return wx.showToast({ title: '没有更多数据了哦~', icon: 'none', duration: 2000 })
    this.setData({ page: this.data.page + 1 })
    monitoryList({
      pagination: {
        firstQueryTime: getYMDHMS(new Date()),
        page: this.data.page,
        rows: 10
      },
      param: {
        venueId: wx.getStorageSync('GYMID'),
        userId: wx.getStorageSync('userId'),
        manuFacturer: this.data.tabList[this.data.active].id,
        name: this.data.inputValue
      }
    }).then(res => {
      if (res.errorCode != '000000') return
      if (res.recordList.length === 0 && this.data.page != 1 && res.recordList) {
        wx.showToast({ title: '没有更多数据了哦~', icon: 'none', duration: 2000 })
        this.setData({ is: false })
        return
      }
      if (res.recordList.length < 10 || !res.recordList) {
        this.setData({ is: false })
      }
      let arr = res.recordList
      let arrs = []
      if (arr) {
        arrs = this.data.list.concat(arr)
      } else {
        arrs = []
      }
      this.setData({ list: arrs, totalCount: res.totalCount, info: res })
    })
  },
  clearList() {
    this.setData({ is: true, page: 0, list: [] })
    this.getList()
  },

  bindKeyInput: function (e) {
    this.setData({
      inputValue: e.detail.value
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.tomanufacturerList()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    //自定义返回值
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    this.getList()
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})